var Aslarea;
CRM = function(obj){	return CRM.anew(obj);}
CRM.anew = function(obj){this.obj=obj;	return this;}

//--客户管理
CRM.customer = function(obj){	return CRM.customer.anew(obj);}
CRM.customer.anew = function(obj){this.obj=obj;	return this;}
CRM.customer.forgo = function(id){
	var url = Aurl.Amodr+'-forgo-'+id;
	jQuery.ajax({
		type:"POST",url:url,data:{"key":id},
		beforeSend: function(){Acan.alert('开始请求');},
		ajaxSend: function(){Acan.alert('发送请求');},
		success: function(data){
			Acan.alert(data,1000);
			setTimeout('Acan.url.refresh()',1000);
		}
	})
}
CRM.customer.area = function(obj){
	var obj = jQuery('input[name="save[area]"]',this);
	obj.after(Ahtml.tag('div',{'class':'_save_area'}));
	var arr;
	var str = obj.val();
	try{
		if(str.length>0 && jQuery.inArray(str.substring(0,1),['{','[','"'])>=0){
			arr = Acan.base.json_obj(obj.val());
		}else{
			arr = '';
		}
	}catch(e){
		arr = '';
	}
	Aslarea = new Apps.sl('._save_area','Aslarea',area.obj,area.data,arr,'CRM.customer.area_save(this)');
	return;
}
CRM.customer.area_save = function(obj){
	var arrs=[];
	var arr = Aslarea.value();
	var title = Aslarea.title();
	for(var i in arr){
		if(arr[i]>0){arrs[i] = arr[i];}
	}
	jQuery('input[name="save[area]"]').val(Acan.base.obj_json(arrs));
	$("#customer form").submit( function () {
		var id = jQuery('input[key="key"]').val();
		var area_str = '';
		for (var i in title) {
			if(area_str.length==0){
				area_str = title[i];
			}else{
				area_str += ','+title[i];
			}
		}
		jQuery('#data-'+id+'-area a').text(area_str);
	});
}
CRM.customer.area_view = function(){
	var areas = Array();
	var str='';
	for(var i in area_id){
		var av = jQuery('#data-'+area_id[i]+'-area span');
		av.hide();
		if(jQuery('#data-'+area_id[i]+'-area a').length<1){
			av.after(Ahtml.tag('a',{}));
		}
		var aval = av.text();
		var areaobj = Acan.base.json_obj(aval);
		var areanext='';
		var areatitle='';
		for(var a in areaobj){
			if(!areanext){
				areanext = areaobj[a];
				areatitle = area.data[a][areaobj[a]];
			}else{
				try{
					if(area.data[a][areanext][areaobj[a]])
						areatitle += ','+area.data[a][areanext][areaobj[a]];
					areanext = areaobj[a];
				}catch(e){}
			}
		}
		jQuery('#data-'+area_id[i]+'-area a').text(areatitle);
	}
}
CRM.customer.change = function(){
	var obj1 = jQuery(".change_client select[name=\"out\"]");
	var obj2 = jQuery(".change_client select[name=\"in\"]");
	if(obj1.length==0){
		obj1 = jQuery(".change_client select[name=\"cids[]\"]");
	}
	if(obj1.val()==null){
		Acan.alert("请选择转出用户!",1000);
		obj1.focus();return;
	}
	if(obj2.val()==null){
		Acan.alert("请选择转入用户!",1000);
		obj2.focus();return;
	}
	if(obj2.val()==obj1.val()){
		Acan.alert("转出/转入客户不能相同!",1000);
		obj2.focus();return;
	}
	var data = jQuery(".change_client form").serialize();
	var url = Aurl.Amodr+'-change_client';
	jQuery.ajax({
		type:"POST",url:url,data:data,
		beforeSend: function(){Acan.alert('开始请求');},
		ajaxSend: function(){Acan.alert('发送请求');},
		success: function(data){
			Acan.alert(data,1000);
			setTimeout('Acan.url.refresh()',1000);
		}
	});
}
CRM.find = function (){}
CRM.find.all = function(){
	var url = Aurl.Amodr+'-findall?'+jQuery("._crmtop").serialize();
	var html = Ahtml.tag("iframe",{"src":url,"class":"a_windows_iframe","frameborder":"0","style":"width:100%;height:98%;"});
	CRM.change.show("搜索结果",html,{width:800,height:500})
}
CRM.change = function(){}
CRM.change.show = function(title,html,css){
	if(!this.Did){
		this.Did = 'ajax_track';
	}
	if(!obj){
		var obj=Object();
	}
	obj.id=this.Did;
	obj["class"]='acan_win dis_none';
	obj.css={height:'auto',width:'auto'};
	if(css){obj.css=css}
	obj._close={onclick:'Acan.close(\''+obj.id+'\')','class':'-close'};//关闭按钮
	Acan.base.win(obj,title,html);
	Acan.ajax_bg();
	return jQuery("#"+obj.id+" ._content");
}
CRM.change.view = function(cids){
	var obj = {};
	if(typeof cids != 'object'){
		obj[cids] = jQuery("#data-"+cids+"-realname").text();
	}else{
		obj = cids;
	}
	var uhtml='',chtml='',html='';
	for(var i in obj){
		chtml += '<option value="'+i+'" selected="selected">'+obj[i]+'</option>';
	}
	for(var i in users){
		uhtml += '<optgroup label="'+users[i].title+'">';
		for(var uid in users[i].user){
			uhtml += '<option value="'+uid+'">'+users[i].user[uid]+'</option>';
		}
	}
	uhtml = Ahtml.tag('p',{},"转入用户")+Ahtml.tag('select',{"name":"in","size":"15","style":"width:100px;"},uhtml);
	chtml = Ahtml.tag('p',{},"转出客户")+Ahtml.tag('select',{"name":"cids[]","multiple":"multiple","size":"15","style":"width:100px;"},chtml);
	html = Ahtml.tag('table',{},
		Ahtml.tag('tr',{},Ahtml.tag('td',{},chtml)+Ahtml.tag('td',{},uhtml))
		+Ahtml.tag('tr',{},Ahtml.tag('td',{"colspan":"2"},Ahtml.tag('input',{"type":"submit","value":"变更"})))
	);
	html = Ahtml.tag('div',{"class":"change_client"},Ahtml.tag('form',{"onsubmit":"CRM.customer.change();return false;","method":"post"},html));
	CRM.change.show("变更客户",html,{width:640,height:480})
}
CRM.change.check = function(){
	var cid,cids = {};
	jQuery("._customer_list [name=\"chk[]\"]:checked").each(function(i){
		cid = jQuery(this).val();
		cids[cid] = jQuery("#data-"+cid+"-realname").text();
	});
	CRM.change.view(cids);
}
CRM.change.save = function(cids){
	
}

CRM.track = function(obj){	return CRM.track.anew(obj);}
CRM.track.anew = function(obj){this.obj=obj;	return this;}
CRM.track.show = function(title,html,css){
	if(!this.Did){
		this.Did = 'ajax_track';
	}
	if(!obj){
		var obj=Object();
	}
	obj.id=this.Did;
	obj["class"]='acan_win dis_none';
	obj.css={height:'auto',width:'auto'};
	if(css){obj.css=css}
	obj._close={onclick:'Acan.close(\''+obj.id+'\')','class':'-close'};//关闭按钮
	Acan.base.win(obj,title,html);
	//Acan.ajax_bg();
	return jQuery("#"+obj.id+" ._content");
}
CRM.track.open = function(url){
	var title="跟进记录";
	var html = Ahtml.tag('iframe',{"class":"a_windows_iframe","src":url,"frameborder":0});
	CRM.track.show(title,html,{width:640,height:320,overflow:"auto"});
	jQuery("#"+this.Did+' iframe').css({width:620,height:300});
}

